﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Serialization;
using Evaluant.Uss.Models;
using System.IO;
using Evaluant.Uss.Memory;
using Evaluant.Uss;
using Evaluant.Uss.ObjectContext;
using System.Reflection;
using Evaluant.Uss.Common;
using NUnit.Mapping.Domain;
using System.Diagnostics;

namespace NUnit
{
    [TestClass]
    public class Core
    {
        [TestMethod]
        public void ShouldSerializeModelAsMetadataXml()
        {
            MemoryProvider mp = new MemoryProvider();
            mp.RegisterMetaData(MetaDataFactory.FromMetaDataFile("..\\..\\..\\NUnit\\PersistenceEngine\\Models\\Common.xml"));
			mp.RegisterMetaData(MetaDataFactory.FromMetaDataFile("..\\..\\..\\NUnit\\PersistenceEngine\\Models\\Proxy.xml"));
            mp.InitializeConfiguration();

            XmlSerializer xs = new XmlSerializer(typeof(Model));
            using (FileStream fs = File.Create("core.metadata.xml"))
            {
                xs.Serialize(fs, mp.CreatePersistenceEngine().Model);
            }

            mp = new MemoryProvider();
            mp.RegisterMetaData(MetaDataFactory.FromMetaDataFile("core.metadata.xml"));
            mp.InitializeConfiguration();
        }

        [TestMethod]
        public void ShouldTransformDomainNames()
        {
            Assert.AreEqual("System:String", ObjectContext.ConvertNamespaceDomainToEuss("System.String"));
            Assert.AreEqual("System:String", ObjectContext.ConvertNamespaceDomainToEuss(typeof(System.String)));
            Assert.AreEqual("System:Collections:Generic:List{System:String}", ObjectContext.ConvertNamespaceDomainToEuss(typeof(List<string>)));
            Assert.AreEqual("System:Collections:Generic:List{System:Collections:Generic:Dictionary{System:String}{TestUSS:Person}}", ObjectContext.ConvertNamespaceDomainToEuss(typeof(List<Dictionary<string, TestUSS.Person>>)));

            List<Assembly> assemblies = new List<Assembly>();
            assemblies.Add(typeof(TestUSS.Person).Assembly);

            Assert.AreEqual(typeof(System.String), ObjectService.ResolveDomainType("System:String", assemblies));
            Assert.AreEqual(typeof(List<string>), ObjectService.ResolveDomainType("System:Collections:Generic:List{System:String}", assemblies));
            Assert.AreEqual(typeof(List<Dictionary<string, TestUSS.Person>>), ObjectService.ResolveDomainType("System:Collections:Generic:List{System:Collections:Generic:Dictionary{System:String}{TestUSS:Person}}", assemblies));
        }

        [TestMethod]
        public void ShouldHandleActivatorFactory()
        {
            IActivator activator = ActivatorFactory.CreateActivator(typeof(Person));
            object o = activator.CreateInstance();

            Assert.AreEqual(typeof(Person), o.GetType());
        }

        [TestMethod]
        public void ShouldBeAbleToLoadOnlyNamespaceOfAssemblies()
        {
            IPersistenceProvider provider = XmlConfigLoader.LoadXmlConfig("engines.config", "Core");
            foreach (Evaluant.Uss.Models.Entity entity in provider.CreatePersistenceEngine().Model.Entities)
            {
                Assert.AreEqual(entity.Type.Substring(0, entity.Type.LastIndexOf(':')), "NUnit:Mapping:Domain");
            }
        }
    }
}
